<template>
  <view>
    <s-layout
      title="首页"
      navbar="111"
      tabbar="/pages/index/index"
      :navbarStyle="template.style?.navbar"
      onShareAppMessage
      :bgStyle="{ backgroundColor: '#fff' }"
    >
      <!-- 搜索框移到轮播图上方 -->
      <view class="search-box">
        <view class="top-nav">
          <!-- 中间切换按钮 -->
          <view class="tab-buttons">
            <view class="tab-btn active">首页</view>
            <view class="tab-btn">到店</view>
          </view>
        </view>

        <view class="search-container">
          <s-search-block
            :placeholder="'搜索关键字'"
            :radius="20"
            width="100%"
            elBackground="#fff"
            @click="sheep.$router.go('/pages/index/search')"
          ></s-search-block>
        </view>
      </view>

      <!--轮播图 - 移到顶部 -->
      <!-- <view class="banner-content-box">
        <swiper
          class="swiper-content"
          style="height: 620rpx"
          :indicator-dots="true"
          :autoplay="true"
        >
          <swiper-item
            v-for="(it, idx) in bannerList"
            style="height: 620rpx"
            :key="it.id"
            @tap="clickBanner(it)"
          >
            <image
              style="width: 100%; height: 620rpx"
              :src="it.picture"
              class="img"
            />
          </swiper-item>
        </swiper>
      </view> -->

      <view class="banner-content-box">
        <swiper
          class="swiper-content"
          style="height: 620rpx"
          :indicator-dots="true"
          :autoplay="true"
        >
          <swiper-item
            v-for="(it, idx) in headMenuList"
            style="height: 620rpx"
            :key="it.icon"
            @tap="clickBanner(it)"
          >
            <image
              style="width: 100%; height: 620rpx"
              :src="it.icon"
              class="img"
            />
          </swiper-item>
        </swiper>
      </view>

      <!-- 分类 -->
      <view class="category-section">
        <swiper style="height: 310rpx" :indicator-dots="true" :autoplay="false">
          <swiper-item
            v-for="(page, pageIndex) in paginatedCategories"
            :key="pageIndex"
          >
            <view class="category-content max-item">
              <view
                class="category-item"
                v-for="(it, idx) in page"
                :key="idx"
                @tap="toGoodsInfo(it)"
              >
                <image :src="it.icon" class="ct-icon" />
                <view class="ct-text">{{ it.name }}</view>
              </view>
            </view>
          </swiper-item>
        </swiper>
      </view>

      <!-- 服务保障模块 -->
      <view class="service-guarantee">
        <view class="service-item">
          <view class="service-icon">🏆</view>
          <text class="service-text">品牌甄选</text>
        </view>
        <view class="service-item">
          <view class="service-icon">🕐</view>
          <text class="service-text">7*24小时无休</text>
        </view>
        <view class="service-item">
          <view class="service-icon">🚚</view>
          <text class="service-text">包邮到家</text>
        </view>
        <view class="service-item">
          <view class="service-icon">↩️</view>
          <text class="service-text">7天无理由</text>
        </view>
      </view>

      <!-- 秒杀模块 -->
      <view class="msbox">
        <view class="msitem">
          <view class="msleft">下单送好礼</view>
        </view>

        <view class="msitem2">
          <view class="msright">1</view>
          <view class="msright">2</view>
        </view>
      </view>

      <!-- 人气推荐 -->
      <view class="popularbox">
        <!-- 标题区域 -->
        <view class="popular-header">
          <view class="fire-icon">🔥</view>
          <text class="popular-title">人气推荐</text>
          <text class="popular-subtitle">精选爆款商品，买过都说好</text>
        </view>

        <!-- 内容区域 -->
        <view class="popular-content">
          <!-- 左侧新人特惠区域 -->
          <view class="newbie-section">
            <!-- 蓝色兔子角色图片 -->
            <image
              src="/static/images/newbie-character.png"
              class="character-img"
            />
          </view>

          <!-- 右侧商品列表 -->
          <view class="products-grid">
            <view class="product-item">
              <image src="/static/images/product1.jpg" class="product-img" />
              <text class="product-name">香氛内衣洗...</text>
              <view class="product-price">
                <text class="price-symbol">🏷</text>
                <text class="price-value">30.90</text>
              </view>
            </view>

            <view class="product-item">
              <image src="/static/images/product2.jpg" class="product-img" />
              <text class="product-name">快充线移动...</text>
              <view class="product-price">
                <text class="price-symbol">🏷</text>
                <text class="price-value">150.00</text>
              </view>
            </view>

            <view class="product-item">
              <image src="/static/images/product3.jpg" class="product-img" />
              <text class="product-name">有机胚芽米...</text>
              <view class="product-price">
                <text class="price-symbol">🏷</text>
                <text class="price-value">44.00</text>
              </view>
            </view>
          </view>
        </view>
      </view>

      <!-- 夏日狂欢购 -->
      <view class="summerbox">
        <view class="sumleft">送秦岭</view>
        <view>夏日狂欢购</view>
        <view class="sumright">精选商品</view>
      </view>
      <!-- 夏日狂欢购img -->
      <view class="summerimg"> </view>

      <!-- 商品推荐 -->
      <view class="goodstatus">
        <scroll-view
          class="scroll-view_H"
          scroll-x="true"
          show-scrollbar="false"
          @scroll="scroll"
        >
          <template v-for="(item, index) in categoryList" :key="item.id">
            <view class="goodstatusitem" v-if="index > 1">
              <view
                class="category-text"
                :class="{ active: activeIndex === index }"
                @click="selectCategory(index, item)"
              >
                {{ item.name }}
              </view>
              <!-- 分隔线，最后一个不显示 -->
              <view
                class="divider"
                v-if="index < categoryList.length - 1"
              ></view>
            </view>
          </template>
        </scroll-view>
      </view>
      <view class="goods-block">
        <s-goods-card
          :key="refreshKey"
          :data="goodsCard.data"
          :styles="goodsCard.style"
        />
      </view>
    </s-layout>
  </view>
</template>

<script setup>
import { computed, nextTick, ref } from "vue";
import {
  onLoad,
  onPageScroll,
  onPullDownRefresh,
  onReachBottom,
  onShareAppMessage,
  onShow,
} from "@dcloudio/uni-app";
import sheep from "@/sheep";
import $share from "@/sheep/platform/share";
//#ifdef H5
import weixin from "@/sheep/libs/sdk-h5-weixin";
//#endif
import { userihuanggangLogin } from "@/sheep/hooks/useModal";

const pageSize = 10;
const paginatedCategories = computed(() => {
  const pages = [];
  for (let i = 0; i < categoryList.value.length; i += pageSize) {
    pages.push(categoryList.value.slice(i, i + pageSize));
  }
  return pages;
});

const text1 = ref(
  "uView UI众多组件覆盖开发过程的各个需求，组件功能丰富，多端兼容。让您快速集成，开箱即用"
);
const bannerList = ref([]);
const ADList = ref([]);
const categoryList = ref([]);
const categoryGoMenu = ref([]);
const headMenuList = ref([
  {
    name: "商户电子券",
    icon: "https://ihuanggang-live-1323882773.cos.ap-nanjing.myqcloud.com//image/2025/07/12/bf8912f59eae4c9a9f50b37151c13740.png",
    url: "",
    class: "dianziquan",
  },
  {
    name: "工会福利",
    icon: "https://ihuanggang-live-1323882773.cos.ap-nanjing.myqcloud.com//image/2025/07/12/db8af78ff71c4ef39376742291e2fe9e.png",
    url: "",
    class: "gonghui",
  },
]);
const goodsCard = {
  data: {
    mode: 2,
    goodsFields: {
      title: {
        show: 1,
      },
      subtitle: {
        show: 1,
      },
      price: {
        show: 1,
      },
      original_price: {
        show: 1,
      },
      sales: {
        show: 1,
      },
      stock: {
        show: 0,
      },
    },
    buyNowStyle: {
      // "mode": 2,
      // "text": "立即购买",
      // "color1": "#E9B461",
      // "color2": "#EECC89",
      // "src": "\/storage\/decorate\/20221115\/4782356b4587dc4f4a218f2540a0bafc.png",
      // "right": '20rpx',
      // "bottom":"18rpx"
    },
    tagStyle: {
      show: 0,
      src: "",
    },
    params: {
      orderField: "sort",
      orderSort: "asc",
    },
    borderRadiusTop: 6,
    borderRadiusBottom: 6,
    space: 8,
  },
  style: {
    background: {
      type: "color",
      bgImage: "",
      bgColor: "",
    },
    marginLeft: 8,
    marginRight: 8,
    marginTop: 0,
    marginBottom: 10,
    borderRadiusTop: 0,
    borderRadiusBottom: 0,
    padding: 0,
  },
};

// 隐藏原生tabBar
uni.hideTabBar();

const template = computed(() => sheep.$store("app").template.home);
const barHeight = ref(0);

onLoad(async (options) => {
  getIndexPage();
  const statusBarHeight = sheep.$platform.device.statusBarHeight;
  barHeight.value = statusBarHeight + 54 + "px";
  init(options);
});

function goTobigCatrgory(item) {
  // sheep.$router.go(item.url)

  if (["苏宁", "京东"].indexOf(item.name) > -1) {
    uni.showToast({
      icon: "none",
      title: "敬请期待",
    });
  } else {
    sheep.$router.go(item.url);
  }
}

function goToCategory(item) {
  sheep.$router.go(`/pages/index/category1?params=${JSON.stringify(item)}`);
}

const goodsstatuslist = ref([]);

const getIndexPage = async () => {
  const res = await sheep.$api.category.getIndexPage();
  console.log(res, "aaaaaaaaaaa");
};

async function getCategoryList() {
  const res = await sheep.$api.category.list();

  categoryList.value = [];
  categoryList.value = res.filter((item) => item.children?.length > 0);
}

function init(options) {
  sheep.$store("app").init();
  setTimeout(function () {
    uni.stopPullDownRefresh();
  }, 800);
  // 开始登录
  // getCategoryList()
  categoryMoudle();
  getCategoryList();
  carouselImageH5List();
  ADImageH5List();
  // userLogin()
  // userihuanggangLogin()

  // 预览模板
  if (options.templateId) {
    sheep.$store("app").init(options.templateId);
  }

  // 解析分享信息
  if (options.spm) {
    $share.decryptSpm(options.spm);
  }

  // 进入指定页面(完整页面路径)
  if (options.page) {
    sheep.$router.go(decodeURIComponent(options.page));
  }

  //#ifdef H5
  setOpenShare();
  //#endif
}
onShareAppMessage((res) => {
  let shareData = {
    title: "邀请好友领取海量现金券!",
    desc: "我正在使用xxxApp，赶紧跟我一起来体验！",
    link: "https://mall.ichengle.top/uni/",
    imgUrl:
      "https://bjetxgzv.cdn.bspapp.com/VKCEYUGU-uni-app-doc/d8590190-4f28-11eb-b680-7980c8a877b8.png",
  };
  return {
    ...shareData,
  };
});

function toGoodsInfo(it) {
  console.log("toGoodsInfo", it);
  if (it.name === "电影") {
    sheep.$router.go("/pages/movie/index");
  } else if (it.name === "蛋糕") {
    sheep.$router.go("/pages/cake/index");
  } else {
    sheep.$router.go("/pages/goods/list", { categoryId: it.id });
  }
}

// 添加一个刷新标识
const refreshKey = ref(0);

// 下拉刷新
onPullDownRefresh(() => {
  sheep.$store("app").init();

  // 重新获取数据
  getCategoryList();
  carouselImageH5List();
  ADImageH5List();
  categoryMoudle();

  // 触发商品列表刷新
  refreshKey.value++;

  setTimeout(function () {
    uni.stopPullDownRefresh();
  }, 800);
});
// 上拉加载更多
onReachBottom(() => {
  // 该事件必须申明,子组件才会触发该事件
});
onPageScroll(() => {});
//#ifdef H5
// 微信h5分享；
function setOpenShare() {
  let currentUrl = location.href; //获取当前页面链接

  let shareData = {
    title: "邀请好友领取海量现金券!",
    desc: "我正在使用xxxApp，赶紧跟我一起来体验！",
    link: "https://mall.ichengle.top/uni/",
    imgUrl:
      "https://bjetxgzv.cdn.bspapp.com/VKCEYUGU-uni-app-doc/d8590190-4f28-11eb-b680-7980c8a877b8.png",
  };
  console.log("更新分享", shareData);
  weixin.updateShareInfo(shareData);
}
//#endif
// 获取商品分类
// function getCategoryList() {
// 	sheep.$api.category.list().then(res => {
// 		// categoryList.value = res
// 	})
// }

// 获取轮播图列表
function carouselImageH5List() {
  sheep.$api.pageIndex.carouselImageH5List().then((res) => {
    bannerList.value = res.content;
    // console.log('carouselImageH5List',res)
  });
}

function ADImageH5List() {
  sheep.$api.pageIndex.ADImageH5List().then((res) => {
    ADList.value = res.content;
    // console.log('ADImageH5List',res)
  });
}

function categoryMoudle() {
  sheep.$api.pageIndex.categoryMoudle().then((res) => {
    categoryGoMenu.value = res;
    // console.log('categoryMoudle',res)
  });
}
// 在现有的 ref 定义中添加
const activeIndex = ref(2); // 默认选中第三个（跳过前两个）

// 添加选择分类的方法
const selectCategory = (index, item) => {
  activeIndex.value = index;
  console.log("点击的item:", item);
  console.log("item的名称:", item.name);
  console.log("item的索引:", index);
  // 这里可以添加其他逻辑
};

// 滚动事件处理（如果需要）
function scroll(e) {
  // 可以在这里处理滚动逻辑
}
</script>

<style lang="scss" scoped>
pages {
  background-color: #d8d8d8;
}
// 搜索框样式
.search-box {
  padding: 40rpx 20rpx 20rpx 20rpx;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 999;
}

.top-nav {
  display: flex;
  justify-content: center;
  margin-bottom: 20rpx;
  height: 60rpx;
  align-items: center;
}

.tab-buttons {
  display: flex;
  background-color: #fff;
  border-radius: 30rpx;
  padding: 4rpx;

  .tab-btn {
    padding: 12rpx 30rpx;
    border-radius: 26rpx;
    color: #333333;
    font-size: 28rpx;
    transition: all 0.3s;

    &.active {
      background: #fe6c07;
      color: #fff;
      font-weight: bold;
    }
  }
}

.search-container {
  width: 100%;
}

// 轮播图样式 - 顶到页面顶部
.banner-content-box {
  width: 100%;

  .swiper-content {
    width: 100%;
  }

  .img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}

// 分类区域
.category-section {
  background-color: #fff;
  position: relative;
  top: -16rpx;
  border-top-left-radius: 30rpx;
  border-top-right-radius: 30rpx;
  overflow: hidden;
}

// 广告区域
.ad-section {
  margin: 20rpx 0;
}

.see_ads {
  margin: 40rpx;
  text-align: center;
  color: red;
}

.dianziquan,
.gonghui {
  width: 80rpx !important;
  height: 80rpx !important;
  margin-top: 5rpx;
}

.shanghu,
.jifen {
  width: 60rpx !important;
  height: 60rpx !important;
  margin-top: 14rpx;
}

.btn-content {
  display: flex;
  justify-content: center;
}

.see-btn {
  width: 190rpx;
  height: 70rpx;
  font-size: 28rpx;
  border: 2rpx solid #dfdfdf;
  border-radius: 35rpx;
  font-weight: 400;
  color: #595959;
}

.goods-block {
  margin: 0 20rpx 10rpx 20rpx;
}

.icon-text {
  display: flex;
  justify-content: center;
  margin-bottom: 24rpx;

  .text-info {
    display: flex;
    align-items: center;
    width: 440rpx;

    .left {
      width: 108rpx;
      height: 2rpx;
      background: #d8d8d8;
    }

    .center {
      width: 128rpx;
      height: 44rpx;
      font-size: 32rpx;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #333333;
      line-height: 44rpx;
      margin-left: 48rpx;
      margin-right: 48rpx;
    }

    .right {
      width: 108rpx;
      height: 2rpx;
      background: #d8d8d8;
    }
  }
}

// 服务保障模块样式
.service-guarantee {
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: #fff;
  padding: 20rpx 20rpx;
  margin: 0 20rpx 20rpx 20rpx;
  border-radius: 20rpx;
}

.service-item {
  display: flex;
  align-items: center;
}

.service-icon {
  font-size: 24rpx;
  margin-right: 4rpx;
  margin-bottom: 4rpx;
}

.service-text {
  font-size: 24rpx;
  color: #666;
  text-align: center;
}

.category-content {
  padding: 20rpx;
  box-sizing: border-box;
  display: flex;
  flex-wrap: wrap;
  background-color: #fff;
  width: 100%;

  .category-item {
    flex: 0 0 20%;
    margin-bottom: 20rpx;
    text-align: center;

    .ct-icon {
      width: 60rpx;
      height: 60rpx;
      border-radius: 50%;
    }

    .ct-text {
      text-align: center;
      margin-top: 10rpx;
      font-size: 28rpx;
      font-weight: bold;
    }
  }

  .max-item {
    flex: 0 0 20%;
    margin-bottom: 0;
  }
}

.msbox {
  display: flex;
  box-sizing: border-box;
  justify-content: space-between;
  padding: 24rpx;
  padding-top: 0;
}
.msitem {
  width: 48%;
  height: 420rpx;
  border-radius: 20rpx;
  background-color: #d8d8d8;
  position: relative;
  overflow: hidden;
}
.msitem2 {
  width: 48%;
  height: 420rpx;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.msright {
  display: flex;
  flex-direction: column;
  height: 200rpx;
  background-color: #fe6c07;
  border-radius: 20rpx;
}
.msleft {
  width: 120rpx;
  text-align: center;
  color: #fff;
  position: absolute;
  height: 40rpx;
  line-height: 40rpx;
  font-size: 20rpx;
  right: 0;
  background-color: #fe6c07;
  border-bottom-left-radius: 20rpx;
}
// 人气推荐模块样式
.popularbox {
  margin: 20rpx;
  background: linear-gradient(135deg, #4fc3f7 0%, #29b6f6 100%);
  border-radius: 20rpx;
  padding: 30rpx;
  position: relative;
  overflow: hidden;
}

.popular-header {
  display: flex;
  align-items: center;
  margin-bottom: 30rpx;
}

.fire-icon {
  font-size: 40rpx;
  margin-right: 15rpx;
}

.popular-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #fff;
  margin-right: 20rpx;
}

.popular-subtitle {
  font-size: 24rpx;
  color: rgba(255, 255, 255, 0.8);
}

.popular-content {
  display: flex;
  background-color: #fff;
  border-radius: 20rpx;
}

.newbie-section {
  width: 167rpx;
}

.newbie-tag {
  background: linear-gradient(45deg, #ff6b35, #ff8a65);
  color: #fff;
  padding: 8rpx 16rpx;
  border-radius: 20rpx;
  font-size: 24rpx;
  font-weight: bold;
  margin-bottom: 20rpx;
}

.character-img {
  width: 120rpx;
  height: 120rpx;
  object-fit: contain;
}

.products-grid {
  flex: 1;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16rpx;
  padding: 20rpx;
  display: flex;
  gap: 15rpx;
}

.product-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.product-img {
  width: 80rpx;
  height: 80rpx;
  border-radius: 12rpx;
  margin-bottom: 15rpx;
  object-fit: cover;
}

.product-name {
  font-size: 24rpx;
  color: #333;
  margin-bottom: 10rpx;
  line-height: 1.2;
}

.product-price {
  display: flex;
  align-items: center;
  justify-content: center;
}

.price-symbol {
  font-size: 20rpx;
  margin-right: 5rpx;
}

.price-value {
  font-size: 28rpx;
  font-weight: bold;
  color: #ff6b35;
}

.summerbox {
  width: 400rpx;
  margin: auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 900;
  font-size: 30rpx;
  margin-top: 30rpx;
}
.sumleft {
  font-size: 20rpx;
  background-color: #ff6b35;
  padding: 8rpx 12rpx;
  color: #fff;
  border-radius: 20rpx;
}
.sumright {
  color: #ff6b35;
  border: 2rpx solid #ff6b35;
  box-sizing: border-box;
  padding: 2rpx 12rpx;
  border-radius: 20rpx;
  font-size: 22rpx;
  margin-top: 4rpx;
}
.summerimg {
  height: 2632rpx;
  background-color: #29b6f6;
  margin-top: 30rpx;
}
.goodstatus {
  background: #f8f8f8;
  padding: 20rpx 0;
}

.scroll-view_H {
  width: 100%;
  white-space: nowrap;
  display: flex;
  overflow-x: auto;
  overflow-y: hidden;
}

.goodstatusitem {
  display: inline-flex;
  align-items: center;
  flex-shrink: 0;
  white-space: nowrap;
}

.category-text {
  font-size: 28rpx;
  color: #333333;
  font-weight: 500;
  padding: 20rpx 30rpx;
  white-space: nowrap;
  cursor: pointer;
  transition: color 0.3s ease;
  display: inline-block;
}

.category-text.active {
  color: #ff6b35;
  font-weight: bold;
}

.divider {
  width: 2rpx;
  height: 30rpx;
  background-color: #e5e5e5;
  margin: 0 10rpx;
  flex-shrink: 0;
}

.category-text:hover {
  opacity: 0.8;
}
</style>
